<template>
  <div>
    <!-- 按钮 -->
    <el-button type="primary" icon="el-icon-plus" style="margin-bottom: 15px"
   @click="addTradeMark">添加</el-button
    >
    <!-- 表单 -->
    <el-table border style="width: 100%" :data="list">
      <el-table-column
        type="index"
        label="序号"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
       prop="tmName"
        label="品牌名称"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column prop="logoUrl" label="品牌LOGO"  width="300" align="center">
          <template slot-scope="{row}">
             <img :src="row.logoUrl" style="width:80px">
          </template>
      </el-table-column>
      <el-table-column  label="操作"  width="300" align="center">
        <template slot-scope="{row}">
            <el-button type="warning" icon="el-icon-edit" size="mini" @click=updateTradeMark(row)>修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click=deleteTradeMark(row)>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 
      -->
    <el-pagination
    style="margin-top:20px"
      :current-page="page"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,total,sizes"
      :total="total"
      @current-change="handleCurrentChange "
      @size-change="handleSizeChange"
      :page-sizes="[3,4,5,6]"></el-pagination>

  <!-- 对话框 -->
<el-dialog :title="tmForm.id?'修改品牌':'添加品牌'" :visible.sync="dialogFormVisible">
  <el-form style="width:80%" :model="tmForm" :rules="rules"  ref="ruleForm">
    <el-form-item label="品牌名称" label-width="100px" prop="tmName">
      <el-input autocomplete="off" v-model.lazy="tmForm.tmName"></el-input>
    </el-form-item>
    
    <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
        <el-upload
          class="avatar-uploader"
          action="/product-api/admin/product/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addupdatetrade()">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
export default {
  name: "TradeMark",
  data(){
    return{
      page:1,
      limit:3,
      //总数据条数
      total:0,
      //列表展示的数组
      list:[],
      //对话框显示和隐藏
     dialogFormVisible: false,
     imageUrl:'',
     //收集表单数据,对象身上的属性，不能瞎写要依照文档
     tmForm:{
     logoUrl:'',
     tmName:'你好吗',
     },
     //表单验证
     rules: {
          tmName: [
            { required: true, message: '请输入品牌名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
          logoUrl: [
            { required: true, message: '请选择品牌图片'}
          ],
     }
    }
  },
   mounted(){
    this.getPageList()
   },
   methods:{
    //1.获取商品列表展示
    getPageList(){
      //结构解构
    const{page,limit}=this
    const result=this.$API.tradeMark.reqtradeMarkList(page,limit)
    result.then((response)=>{
      if(response.code===200){
      this.total=response.data.total
       this.list=response.data.records
      }
   },error=>{
    console.log(error.message)
   })
    },
    //2.分页器点击
    handleCurrentChange(page){
        this.page=page
        this.getPageList()
    },
    handleSizeChange(limit){
        this.limit=limit
        this.getPageList()
    },
    //3.修改某个品牌
    updateTradeMark(row){
      //row当前用户点击的信息
      this.dialogFormVisible=true
      this.tmForm={...row}
 
    },
    //4.添加某个品牌
    addTradeMark(){
      this.dialogFormVisible=true
      this.tmForm={tmName:'',logoUrl:''}
    },
    //5.上传图片相关回调
      handleAvatarSuccess(res, file) {
        this.tmForm.logoUrl = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
    },
    //6.添加或者修改品牌
    addupdatetrade(){
    //当所有表单验证通过再发送请求
      this.$refs.ruleForm.validate(async (valid) => {
      if (valid) {
       this.dialogFormVisible=false
       let result=await this.$API.tradeMark.reqAddandUpdataTradeMark(this.tmForm)
      if(result.code===200){
        this.$message({
          type:'success',
          message:this.tmForm.id?'修改品牌成功':'添加品牌成功'
        })
        let pagess=null
        if(this.total%this.limit==0){
          pagess=Math.ceil(this.total/this.limit)+1
        }else{
          pagess=Math.ceil(this.total/this.limit)
        }
        this.handleCurrentChange(this.tmForm.id?this.page:pagess)

        // this.getPageList()
      }
       } else {
            alert('提交失败!!');
            return false;
          }
        });
      },
    //7.删除某个品牌
   deleteTradeMark(row){

    this.$confirm(`你确定要删除${row.tmName}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
        let result=await this.$API.tradeMark.reqdeletetradeMark(row.id)
        if(result.code===200){
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
           this.handleCurrentChange(this.list.length>1?this.page:this.page-1)
        }else{
      alert('删除操作失败！')
    }
    }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
    }
   }
</script>

<style>
.el-pagination{
    text-align: center;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>